package com.sharemarking.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sharemarking.system.dto.orgManagement.OrgVerifyInfoDto;

import com.sharemarking.system.dto.orgVerify.OrgVerifyListDto;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.Org;
import com.sharemarking.wa.common.entity.OrgVerify;
import com.sharemarking.wa.common.entity.OrgVerifyAttachment;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface OrgVerifyMapper extends BaseMapper<OrgVerify> {

    List<OrgVerifyListDto> getProductWasteOrgList(Map map);

    Integer getProductWasteOrgListTotal(Map map);

    List<OrgVerifyListDto> getManagementOrgList(Map map);

    Integer getManagementOrgListTotal(Map map);

    @Select("SELECT attachment FROM wa_org_verify_attachment WHERE org_verify_id = #{id} AND type = 0")
    List<String> getAttachList(int id);

    @Select("SELECT attachment FROM wa_org_verify_attachment WHERE org_verify_id = #{id} AND type = 1 LIMIT 1")
    String getAttach(int id);

    @Select("SELECT a.*,IFNULL(b.real_name,b.nick_name)AS memberName,b.mobile,c.is_monthly_pay,c.id AS orgId,is_monthly_pay " +
            "FROM wa_org_verify a LEFT JOIN sys_member b ON a.member_id = b.id " +
            "LEFT JOIN sys_org c ON a.org_id = c.id WHERE a.id = #{id}")
    OrgVerifyInfoDto getInfo(Integer orgVerifyId);

    @Select("SELECT COUNT(*) FROM sys_member WHERE mobile = #{mobile}")
    Integer isBindMobile(String mobile);

    @Select("SELECT COUNT(*) FROM sys_member WHERE mobile = #{mobile} AND id=#{memberId}")
    Integer isBindMobileByMemberId(@Param("mobile")String mobile,@Param("memberId") Integer memberId);

    @Insert("INSERT sys_member(real_name,mobile,role,password,create_date,nick_name,head_img) VALUES(#{realName},#{mobile},#{role},#{password},#{createDate},#{nickName},#{headImg}) ")
    @Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
    void insertMember(Member member);

    @Update("UPDATE sys_member SET real_name=#{realName},mobile=#{mobile},update_date=#{updateDate} WHERE id=#{id}")
    void updateMember(Member member);

    @Insert("INSERT sys_org(member_id,name,real_name,create_date,type) VALUES (#{memberId},#{name},#{realName},#{createDate},#{type})")
    @Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
    void insertOrg(Org org);

    @Update("UPDATE sys_org SET name=#{name},real_name=#{realName},update_date=#{updateDate},type=#{type},is_monthly_pay = #{isMonthlyPay} WHERE id=#{id}")
    void updateOrg(Org org);

    @Select("SELECT member_id FROM wa_org_verify WHERE id = #{orgVerifyId}")
    Integer getMemberId(Integer orgVerifyId);

    List<OrgVerifyListDto> getTransportOrgList(Map map);

    Integer getTransportOrgListTotal(Map map);

    @Select("SELECT a.real_name FROM sys_org a LEFT JOIN wa_org_verify b ON a.id = b.org_id " +
            "WHERE b.exam_state = 1")
    List<String> getPassVerifyOrgList();

    @Insert("INSERT INTO sys_org_member (org_id,member_id,create_date) VALUES (#{orgId},#{memberId},NOW())")
    void insertOrgMember(@Param("orgId") Integer orgId,@Param("memberId") Integer memberId);

    @Update("UPDATE sys_org SET real_name = #{realName} WHERE id = #{orgId} ")
    void updateOrgRealName(@Param("orgId") Integer orgId,@Param("realName")String realName);

    @Select("SELECT waste_type_name FROM wa_org_waste_type WHERE org_verify_id = #{orgVerifyId}")
    List<String> getOrgWasteTypeList(Integer orgVerifyId);

    @Select("SELECT name FROM wa_waste_type")
    List<String> getWasteTypeNameList();

    @Delete("DELETE FROM wa_org_verify_attachment WHERE org_verify_id = #{id}")
    void deleteAttach(Integer id);

    @Insert("INSERT wa_org_verify_attachment(org_verify_id,attachment,type,create_date) VALUES(#{orgVerifyId},#{attachment},#{type},#{createDate})")
    void insertAttach(OrgVerifyAttachment orgVerifyAttachment);

    @Select("SELECT a.id,a.real_name AS name,b.real_name AS memberName,b.mobile FROM sys_org a  \n" +
            "LEFT JOIN sys_member b ON a.member_id = b.id\n" +
            "WHERE a.id = #{id}")
    OrgVerifyInfoDto getTransportOrgInfo(Integer id);

    @Update("UPDATE sys_org SET real_name = null WHERE id = #{orgId}")
    void updateOrgRealNameNull(Integer orgId);

    @Select("SELECT IFNULL(real_name,name) FROM sys_org WHERE id = #{orgId}")
    String getNameByOrg(Integer orgId);
}
